import random


class Weapon:
    def __init__(self, name, power):
        self.name = name
        self.power = power

    def __str__(self):
        return self.name


class Monster:
    def __init__(self, name, health):
        self.name = name
        self.health = health

    def __str__(self):
        return self.name


class Room:
    def __init__(self, name, description, connections=None):
        self.name = name
        self.description = description
        self.connections = connections or {}

    def enter(self):
        print(self.description)

    def __str__(self):
        return self.name


class Player:
    def __init__(self, health=10, weapon=None):
        self.health = health
        self.weapon = weapon or Weapon("木剑", 1)

    def attack(self):
        return random.randint(1, 6) + self.weapon.power

    def __str__(self):
        return f"玩家(HP: {self.health}, 武器: {self.weapon})"


class Dungeon:
    def __init__(self):
        self.rooms = {}
        self.create_rooms()

    def create_rooms(self):
        # 创建房间
        self.rooms["入口"] = Room("入口", "你站在地下城的入口，前方一片漆黑。")
        self.rooms["走廊"] = Room("走廊", "长长的走廊延伸到远方，你看到了一扇门。")
        self.rooms["武器室"] = Room("武器室", "这里散落着各种武器。")
        self.rooms["楼梯"] = Room("楼梯", "楼梯蜿蜒向下，通向未知的深处。")
        self.rooms["地下室"] = Room("地下室", "这里是地下城的最底层，空气中弥漫着霉味。")
        self.rooms["藏宝室"] = Room("藏宝室", "满是金银财宝的房间，但在角落里潜伏着危险。")

        # 建立单向连接
        self.rooms["入口"].connections = {"east": self.rooms["走廊"]}
        self.rooms["走廊"].connections = {"west": self.rooms["入口"], "east": self.rooms["武器室"], "south": self.rooms["楼梯"]}
        self.rooms["武器室"].connections = {"west": self.rooms["走廊"]}
        self.rooms["楼梯"].connections = {"north": self.rooms["走廊"], "down": self.rooms["地下室"]}
        self.rooms["地下室"].connections = {"up": self.rooms["楼梯"], "east": self.rooms["藏宝室"]}
        self.rooms["藏宝室"].connections = {"west": self.rooms["地下室"]}


class Game:
    def __init__(self):
        self.player = Player()
        self.dungeon = Dungeon()
        self.current_room = self.dungeon.rooms["入口"]

    def start_game(self):
        while True:
            self.current_room.enter()
            if self.current_room.name == "藏宝室":
                print("你找到了宝藏！你赢了！")
                break

            # 检查是否有怪物
            if random.choice([True, False]):
                monster = Monster(random.choice(["骷髅", "巨鼠", "地精"]), random.randint(1, 2))
                print(f"你遇到了一只 {monster}!")
                if not self.fight(monster):
                    print("你被击败了...")
                    break

            # 检查是否有武器
            if self.current_room.name == "武器室":
                new_weapon = Weapon(random.choice(["铁剑", "银剑", "金剑", "龙牙剑"]), random.randint(2, 5))
                if new_weapon.power > self.player.weapon.power:
                    self.player.weapon = new_weapon
                    print(f"你找到了一把更好的武器：{new_weapon}")

            move = input("请输入你想要去的方向(up/down/north/south/east/west): ")
            if move in self.current_room.connections:
                self.current_room = self.current_room.connections[move]
            else:
                print("你不能朝那个方向走。")

    def fight(self, monster):
        while monster.health > 0 and self.player.health > 0:
            player_attack = self.player.attack()
            monster_roll = random.randint(1, 6)
            print(f"你掷出了 {player_attack} 点，{monster}掷出了 {monster_roll} 点.")

            if player_attack > monster_roll:
                monster.health -= 1
                print(f"{monster}受到了伤害，剩余生命值 {monster.health}.")
            else:
                self.player.health -= 1
                print(f"你受到了伤害，剩余生命值 {self.player.health}.")
        return self.player.health > 0


if __name__ == "__main__":
    game = Game()
    game.start_game()
